//jquery插件autocomplete-ui获取到json数据的js(待诊列表)
var statu = null;
$(function() {
	function getJsonData() {
		$("#example").html("");
		$.getJSON("clinic-record!returnJsonData.action", function(json) {
			$.each(json, function(i) {
				var obj = "\/Date(" + json[i].registerTime + ")\/";
				var time = eval(obj.replace(/\/Date\((\d+)\)\//gi,
						"new Date($1)"));
				var registerTime = time.getYear() + "-" + (time.getMonth() + 1)
						+ "-" + time.getDate();
				if (today == registerTime) {
					if (json[i].registerType == 0) {
						json[i].registerType = "门诊";
					} else if (json[i].registerType == 1) {
						json[i].registerType = "急诊";
					}
					var data = $("#tags").val();
					if (data == json[i].patient.idNumber) {
						$("#example").append(
								"<tr onclick='tdClick(" + json[i].id + "," + 0
										+ ")'>" + "<td>" + json[i].id + "</td>"
										+ "<td width='80px'>"
										+ json[i].patient.idNumber + "</td>"
										+ "<td>" + json[i].patient.name
										+ "</td>" + "<td>"
										+ json[i].registerType + "</td>"
										+ "</tr>");
					}
				}
			});
		});
	}

	$("#tags").autocomplete({
		source : "clinic-record!ajaxRequest.action",
		minLength : 2,
		select : function(event, ui) {
			ui.item ? getJsonData() : null;
		}
	});

});
// //jquery插件autocomplete-ui获取到json数据的js(已诊列表)
$(function() {
	function getJson() {
		$("#exampleClinic").html("");
		$.getJSON("clinic-record!jsonDataDitail.action", function(json) {
			$.each(json, function(i) {
				diaglosis = json[i].diaglosis;
				var obj = "\/Date(" + json[i].registerTime + ")\/";
				var time = eval(obj.replace(/\/Date\((\d+)\)\//gi,
						"new Date($1)"));
				var registerTime = time.getYear() + "-" + (time.getMonth() + 1)
						+ "-" + time.getDate();
				if (today == registerTime) {
					if (json[i].registerType == 0) {
						json[i].registerType = "门诊";
					} else if (json[i].registerType == 1) {
						json[i].registerType = "急诊";
					}
					var data = $("#idNumberOne").val();
					if (data == json[i].patient.idNumber) {
						$("#exampleClinic").append(
								"<tr onclick='tdClick(" + json[i].id + "," + 1
										+ ")'>" + "<td>" + json[i].id + "</td>"
										+ "<td width='80px'>"
										+ json[i].patient.idNumber + "</td>"
										+ "<td>" + json[i].patient.name
										+ "</td>" + "<td>"
										+ json[i].registerType + "</td>"
										+ "</tr>");
					}
				}
			});
		});
	}

	$("#idNumberOne").autocomplete({
		source : "clinic-record!resendAjaxRequst.action",
		minLength : 2,
		select : function(event, ui) {
			ui.item ? getJson() : null;
		}
	});

});

// 待诊列表点击
function tdClick(id, status) {
	statu = status;
	/* 增加一个点击事件 - 使被选中的列具有相应的背景颜色 */
	if (0 == status) {
		$("#exampleClinic tr").removeClass('bg');
		$("#example tr").click(function() {
			$("#example tr").removeClass('bg');
			$(this).addClass('bg');

		});
		detialMessage(id);
	}
	if (1 == status) {
		$("#example tr").removeClass('bg');
		$("#exampleClinic tr").click(function() {
			$("#exampleClinic tr").removeClass('bg');
			$(this).addClass('bg');

		});
		detialMessage(id);
	    $("#texte").val(diaglosis);
	    
	    //选中已诊记录，列出相应的检查结果记录
		$.getJSON("exam-record!checkExamRecord.action",{id:id},function(json){
			//输出返回的json数据
			$("#examRecord").html("");
			$.each(json,function(i){
				$("#examRecord").append(
				"<tr>"+
				"<td>"+json[i].item.term+"</td>"+
				"<td>"+json[i].qualitation+"</td>"+ 
				"<td>"+json[i].examResult+"</td>"+
				"<td>"+json[i].unit+"</td>"+
				"<td>"+json[i].explanation+"</td>"+
				"<td>"+json[i].referenceStandard+"</td>"+
				"<td>"+"<a href='#' onclick='deleteExamRecord("+json[i].id+")'>"+"删除"+"</a>"+"</td>"+
				"</tr>"
				);	
			});
		});
		
		//选中已诊记录，列出相应的用药记录
		$("#medication").html("");
		$.getJSON("medication!checkRecords.action",{id:id},function(json){
			$.each(json,function(i){
				if(json[i].drug.id == 9){
					json[i].drug.id = "西药";
				}else if(json[i].drug.id == 8){
					json[i].drug.id = "中药";
				}
				$("#medication").append(
				"<tr>"+"<td>"+json[i].drug.id+"</td>"+
				       "<td>"+json[i].drug.term+"</td>"+
				       "<td>"+"一天"+json[i].frequency+"次"+"</td>"+
				       "<td>"+json[i].singleDose+"ml"+"</td>"+
				       "<td>"+"<a href='#' onclick='javascript:deleteMedication("+json[i].id+")'>"+"删除"+"</a>"+"</td>"+
				"</tr>"       
				);	
			});
		});
	}

};

// 患者的基本详细信息
function detialMessage(id) {
	$("#clinicId").val(id);
	$.getJSON("clinic-record!returnTeatcher.action", {
		id : id
	}, function(json) {
		if (json.length > 5) {
			$("#bzName").text(json[json.length - 7]);
			$("#zdName").text(json[json.length - 6]);
		}else{
			$("#bzName").html("");
			$("#zdName").html("");
		}
		$("#birthday").text(json[json.length - 2]);
		$("#name").text(json[json.length - 3]);
		$("#clazz").text(json[json.length - 4]);
		$("#sex").text(json[json.length - 5]);
		$("#text2").attr("value", json[json.length - 1]);
	});
	$.getJSON("clinic-record!saveId.action", {
		id : $("#clinicId").val()
	});
	;
}
// 保存诊疗记录
function saveClinicRecord() {
	if ("" == $("#clinicId").val()) {
		alert("还没有选择就诊患者!");
		if ("" == $("#texte").val()) {
			alert("无任何诊断结论!");
		}
	} else {
		jQuery.post("clinic-record!save.action", {
			id : $("#clinicId").val(),
			diagnosis : $("#texte").val()
		}, function(jsonData) {
			$.each(jsonData, function(i) {
				$("#message").append("<span>" + jsonData[i] + "</span>");
			});
		});
	}

}
// 诊疗患者结束
function updateClinicRecord() {
	if ("" == $("#clinicId").val()) {
		alert("还没有选择就诊患者!");
	} else {
		$.getJSON("clinic-record!updateClinicRecord.action", {
			id : +$("#clinicId").val()
		}, function(jsonData) {
			$.each(jsonData, function(i) {
				$("#message").append("<span>" + jsonData[i] + "</span>");
			});
		});
		if (statu != 1) {
			window.location.href = "clinic-record.action";
		}
	}
}

function set(term) {
	/* 增加一个点击事件 - 使被选中的列具有相应的背景颜色 */
	$("#checkId tr").click(function() {
		$("#checkId tr").removeClass('bg');
		$(this).addClass('bg');
	});
	$("#termId").val(term);
}

// 跳转到转院管理模块
function referral() {
	if ("" == $("#clinicId").val()) {
		alert("未选中任何患者，不能进行转院操作!");
	} else {
		window.location.href = "referral!input.action?clicicId="
				+ $("#clinicId").val();
	}

}

// 跳转到病假管理模块
function leave() {
	if ("" == $("#clinicId").val()) {
		alert("未选中任何患者，不能进行请假处理!");
	} else {
		window.location.href = "leave!input.action?clicicId="
				+ $("#clinicId").val();
	}

}

function saveData() {
	$.ajax({
		type : "get",
		url : "medication!input.action?term=" + $("#parentId").val(),
		dataType : "html"

	});

}
